package com.canaan.business.service.basic.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.canaan.business.domain.basic.WmsBaseFactory;
import com.canaan.business.domain.basic.WmsStorehouse;
import com.canaan.business.mapper.basic.WmsBaseFactoryMapper;
import com.canaan.business.service.basic.IWmsBaseFactoryService;
import com.canaan.business.service.basic.IWmsStorehouseService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 工厂管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-28
 */
@Service
public class WmsBaseFactoryServiceImpl implements IWmsBaseFactoryService
{
    @Resource
    private WmsBaseFactoryMapper wmsBaseFactoryMapper;
    @Resource
    private IWmsStorehouseService wmsStorehouseService;

    /**
     * 查询工厂管理
     *
     * @param id 工厂管理主键
     * @return 工厂管理
     */
    @Override
    public WmsBaseFactory selectWmsBaseFactoryById(Long id)
    {
        return wmsBaseFactoryMapper.selectWmsBaseFactoryById(id);
    }

    /**
     * 查询工厂管理列表
     *
     * @param wmsBaseFactory 工厂管理
     * @return 工厂管理
     */
    @Override
    public List<WmsBaseFactory> selectWmsBaseFactoryList(WmsBaseFactory wmsBaseFactory)
    {
        return wmsBaseFactoryMapper.selectWmsBaseFactoryList(wmsBaseFactory);
    }

    /**
     * 新增工厂管理
     *
     * @param wmsBaseFactory 工厂管理
     * @return 结果
     */
    @Override
    public int insertWmsBaseFactory(WmsBaseFactory wmsBaseFactory)
    {
        wmsBaseFactory.setCreateTime(DateUtils.getNowDate());
        return wmsBaseFactoryMapper.insertWmsBaseFactory(wmsBaseFactory);
    }

    /**
     * 修改工厂管理
     *
     * @param wmsBaseFactory 工厂管理
     * @return 结果
     */
    @Override
    public int updateWmsBaseFactory(WmsBaseFactory wmsBaseFactory)
    {
        wmsBaseFactory.setUpdateTime(DateUtils.getNowDate());
        return wmsBaseFactoryMapper.updateWmsBaseFactory(wmsBaseFactory);
    }

    /**
     * 批量删除工厂管理
     *
     * @param ids 需要删除的工厂管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsBaseFactoryByIds(Long[] ids)
    {
        if (ids.length>0){
            //判断工厂是否已绑定数据
            for(int i=0;i<ids.length;i++){
                WmsBaseFactory factory = this.selectWmsBaseFactoryById(ids[0]);
                if (factory!=null){
                   List<WmsStorehouse> storehouseList = this.wmsStorehouseService.selectByFactory(factory.getNo());
                   if (CollectionUtil.isNotEmpty(storehouseList)){
                       throw new ServiceException("该工厂："+factory.getNo()+"已绑定仓库，请先解除仓库与工厂的绑定");
                   }
                }
            }
        }
        return wmsBaseFactoryMapper.deleteWmsBaseFactoryByIds(ids);
    }

    /**
     * 删除工厂管理信息
     *
     * @param id 工厂管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsBaseFactoryById(Long id)
    {
        return wmsBaseFactoryMapper.deleteWmsBaseFactoryById(id);
    }

}
